<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * App\Models\Product
 *
 * @property int $product_id
 * @property int $goods_id 商品id
 * @property string $name 产品名
 * @property string $title 标题
 * @property string $bn 货码
 * @property float $discount_price 打折金额
 * @property float $price 价格
 * @property int $stock 库存
 * @property int $p_order 排序
 * @property int $disabled 是否删除或者禁用 0-否 1-是
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereBn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereDisabled($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereDiscountPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereGoodsId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereProductId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereStock($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Product whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Product extends Model
{
    protected $primaryKey = 'prodcut_id';
    protected $guarded = [];


    /**获得包含sku属性列表的 sku列表
     * @param $goods_id
     */
    public function get_sku_prop_data($goods_id)
    {
        $data = [];
        $property_data = [];
        $product_id_arr = array_column(
            Product::query()->select('product_id')->distinct()->where([['goods_id',$goods_id],['disabled','0']])->get()->toArray(),
            'product_id'
        );
        foreach($product_id_arr as $k => $sku_id){
            $sku_pro_list = DB::table('sku_props_vals AS pro_real')
                -> select('pro.name AS props_name','pro.props_id','pro_val.name AS props_value_name','pro_val.props_val_id AS props_value_id')
                -> join('props AS pro','pro.props_id','=','pro_real.props_id')
                -> join('props_vals AS pro_val','pro_val.props_val_id','=','pro_real.props_val_id')
                -> where([
                    ['pro_real.disabled','0'],
                    ['pro_val.disabled','0'],
                ])
                -> where('sku_id',$sku_id)
                -> get()
                -> toArray();
            $data[$k]['sku_prop_list'] = $sku_pro_list;
            $data[$k]['sku']           = Product::query()->where('product_id',$sku_id)->first()->toArray();
            foreach($data[$k]['sku_prop_list'] as $kk => $property){

                if(!isset($property_data[$property->props_id])){
                    $property_data[$property->props_id]['props_id']  = $property->props_id;
                    $property_data[$property->props_id]['props_name'] = $property->props_name;
                }
                $property_data[$property->props_id][] = $property->props_value_id;
            }
        }
        return [$data,$property_data,$product_id_arr];
    }
}
